package io.renren.modules.substation.controller;

import java.util.*;

import io.renren.modules.substation.entity.TInfoVariableInfoEntity;
import io.renren.modules.substation.service.TInfoVariableInfoService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.substation.entity.TBusiAreaHotInfoEntity;
import io.renren.modules.substation.service.TBusiAreaHotInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 区域热点信息表
 *
 * @author yzx
 * @email sunlightcs@gmail.com
 * @date 2020-07-12 01:12:17
 */
@RestController
@RequestMapping("substation/tbusiareahotinfo")
public class TBusiAreaHotInfoController extends AbstractController {
    @Autowired
    private TBusiAreaHotInfoService tBusiAreaHotInfoService;
    @Autowired
    private TInfoVariableInfoService tInfoVariableInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tbusiareahotinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tBusiAreaHotInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("substation:tbusiareahotinfo:info")
    public R info(@PathVariable("id") Long id){
		TBusiAreaHotInfoEntity tBusiAreaHotInfo = tBusiAreaHotInfoService.getById(id);
        List<String> variableList = new ArrayList<>();
        if (tBusiAreaHotInfo.getVariableInfo() != null && tBusiAreaHotInfo.getVariableInfo().length() > 0){
            String[] variableStr = tBusiAreaHotInfo.getVariableInfo().split(",");
            for (String s : variableStr){
                variableList.add(s);
            }
            tBusiAreaHotInfo.setVariableList(variableList);
        }
        return R.ok().put("tBusiAreaHotInfo", tBusiAreaHotInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tbusiareahotinfo:save")
    public R save(@RequestBody TBusiAreaHotInfoEntity tBusiAreaHotInfo){
        String variableStr = "";
        if (tBusiAreaHotInfo.getVariableList() != null){
            for (String s : tBusiAreaHotInfo.getVariableList()){
                variableStr += s + ",";
            }
            if (variableStr.length() > 0)
                variableStr = variableStr.substring(0, variableStr.length() - 1);
        }
        tBusiAreaHotInfo.setVariableInfo(variableStr);
        tBusiAreaHotInfo.setVariableDesc(variableStr);
        tBusiAreaHotInfo.setCreateOpr(getUserId());
        tBusiAreaHotInfo.setCreateTime(new Date());
        tBusiAreaHotInfoService.save(tBusiAreaHotInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tbusiareahotinfo:update")
    public R update(@RequestBody TBusiAreaHotInfoEntity tBusiAreaHotInfo){
        String variableStr = "";
        if (tBusiAreaHotInfo.getVariableList() != null){
            for (String s : tBusiAreaHotInfo.getVariableList()){
                variableStr += s + ",";
            }
            if (variableStr.length() > 0)
                variableStr = variableStr.substring(0, variableStr.length() - 1);
        }
        tBusiAreaHotInfo.setVariableInfo(variableStr);
        tBusiAreaHotInfo.setVariableDesc(variableStr);
        tBusiAreaHotInfo.setLastOpr(getUserId());
        tBusiAreaHotInfo.setLastTime(new Date());
		tBusiAreaHotInfoService.updateById(tBusiAreaHotInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tbusiareahotinfo:delete")
    public R delete(@RequestBody Long[] ids){
		tBusiAreaHotInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
